package hz.mobileproject.pda.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.constant.RegexConstants;
import com.blankj.utilcode.util.ToastUtils;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.utils.SettingFileUtil;

/**
 * Created by hasee on 2018/1/12.okloo
 */

public class TestSettingActivity extends AppCompatActivity {

    //广西 高测试
    public static String BASE_URL = "http://192.168.10.113:8090";



    //广西南宁正式版本 （电子点名  教育谈话）
    public final static String NOW_IP = "192.168.10.113";



    public final static String TEST_IP = "192.168.10.113";
    //四监测试
//    public final static String TEST_IP = "172.20.20.135";

    public final static String LOCAL_IP = "192.168.10.113";
    public final static String OUT_IP = "192.168.10.113";
    //    public final static String NOW_VERSION = "2.6";
    public final static String NOW_VERSION = "2.8";   //四监正式版本2.6.2   6-12打包2.7   7-4打包2.8
    public final static String TEST_VERSION = "2.8.2";
    public final static String LOCAL_VERSION = "2.5.2";
    public final static String OUT_VERSION = "2.5";
    public final static String PORT = "8090";
    //四监内网IP
    public final static String FOUTRH_PRISION_IP = "172.20.2.70";
    //四监内网版本
    public final static String FOUTRH_PRISION_VERSION = "2.6.2";

    private RadioGroup mRgUrl;
    private Button mBtOk;
    private EditText mEtIp;
    private EditText mEtPort;
    private EditText mEtVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_setting);
        initView();
        initThings();
        initData();
    }

    private void initView() {
        mRgUrl = (RadioGroup) findViewById(R.id.rg_test_set_url);
        mBtOk = (Button) findViewById(R.id.bt_test_set_ok);
        mEtIp = (EditText) findViewById(R.id.et_test_set_ip);
        mEtPort = (EditText) findViewById(R.id.et_test_set_port);
        mEtVersion = (EditText) findViewById(R.id.et_test_set_version);
    }

    private void initData() {
        String baseUrl = SettingFileUtil.getProperties("base_url");
        String baseIp = SettingFileUtil.getProperties("base_ip");
        String version = SettingFileUtil.getProperties("version");
        if (baseIp.equals(NOW_IP)
                && baseUrl.equals("http://" + NOW_IP + ":" + PORT)
                && version.equals(NOW_VERSION)) {
            mRgUrl.check(R.id.rb_test_set_now);
        } else if (baseIp.equals(TEST_IP)
                && baseUrl.equals("http://" + TEST_IP + ":" + PORT)
                && version.equals(TEST_VERSION)) {
            mRgUrl.check(R.id.rb_test_set_test);
        } else if (baseIp.equals(LOCAL_IP)
                && baseUrl.equals("http://" + LOCAL_IP + ":" + PORT)
                && version.equals(LOCAL_VERSION)) {
            mRgUrl.check(R.id.rb_test_set_local);
        } else if (baseIp.equals(OUT_IP)
                && baseUrl.equals("http://" + OUT_IP + ":" + PORT)
                && version.equals(OUT_VERSION)) {
            mRgUrl.check(R.id.rb_test_set_out);
        } else {
            mRgUrl.check(R.id.rb_test_set_cos);
            try {
                mEtIp.setText(baseIp);
                mEtPort.setText(baseUrl.split(":")[2]);
                mEtVersion.setText(version);
                mEtIp.setSelection(mEtIp.getText().toString().length());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        RadioButton rb = (RadioButton) findViewById(mRgUrl.getCheckedRadioButtonId());
        rb.setText(rb.getText().toString() + "(当前选择)");
    }

    private void initThings() {
        mRgUrl.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_test_set_now:
                        setIpAndPort(NOW_IP, PORT, NOW_VERSION);
                        break;
                    case R.id.rb_test_set_test:
                        setIpAndPort(TEST_IP, PORT, TEST_VERSION);
                        break;
                    case R.id.rb_test_set_local:
                        setIpAndPort(LOCAL_IP, PORT, LOCAL_VERSION);
                        break;
                    case R.id.rb_test_set_out:
                        setIpAndPort(OUT_IP, PORT, OUT_VERSION);
                        break;
                    case R.id.rb_test_set_cos:
                        mEtIp.setText("");
                        mEtPort.setText("");
                        mEtVersion.setText("");
                        mEtIp.setEnabled(true);
                        mEtPort.setEnabled(true);
                        mEtVersion.setEnabled(true);
                        break;
                }
                setCurrViewColor();
            }
        });

        mBtOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ip = mEtIp.getText().toString();
                String port = mEtPort.getText().toString();
                String version = mEtVersion.getText().toString();

                if (!ip.matches(RegexConstants.REGEX_IP)) {
                    ToastUtils.showLong("请输入正确的地址");
                    return;
                }

                SettingFileUtil.setProperties("base_url", "http://" + ip + ":" + port);
                SettingFileUtil.setProperties("base_ip", ip);
                SettingFileUtil.setProperties("version", version);
                //重置ip
                Constance.BASE_URL = "http://" + ip + ":" + port;
                Constance.BASE_URL_NOPORT = ip;
                Api.cleanGankService();
                ToastUtils.showLong("修改成功");
                TestSettingActivity.this.finish();
            }
        });
    }

    private void setCurrViewColor() {
        int id = mRgUrl.getCheckedRadioButtonId();
        RadioButton currRb = (RadioButton) findViewById(id);
        for (int i = 0; i < mRgUrl.getChildCount(); i++) {
            RadioButton rb = (RadioButton) mRgUrl.getChildAt(i);
            if (mRgUrl.getChildAt(i).equals(currRb)) {
                rb.setTextColor(Color.parseColor("#FE3F80"));
            } else {
                rb.setTextColor(Color.parseColor("#000000"));
            }
        }
    }

    private void setIpAndPort(String ip, String port, String version) {
        mEtIp.setText(ip);
        mEtIp.setEnabled(false);
        mEtPort.setText(port);
        mEtPort.setEnabled(false);
        mEtVersion.setText(version);
        mEtVersion.setEnabled(false);
    }
}
